package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        if (categoryPageQueryDTO.getPage() == 0) {
            categoryPageQueryDTO.setPage(1);
        }
        if (categoryPageQueryDTO.getPageSize() == 0) {
            categoryPageQueryDTO.setPageSize(5);
        }
//        PageHelper.startPage(1,10);
//        categoryPageQueryDTO.setPage(1);
//        categoryPageQueryDTO.setPageSize(10);
//        Page<Category> page = ctegoryMapper.page(categoryPageQueryDTO);
//        int pages = page.getPages();
//        if(categoryPageQueryDTO.getPage()>pages){
//            categoryPageQueryDTO.setPage(pages);
//        }


        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page1 = categoryMapper.page(categoryPageQueryDTO);

        List<Category> result = page1.getResult();
        long total = page1.getTotal();
        PageResult pageResult = new PageResult(total, result);
        return Result.success(pageResult);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return
     */
    @Transactional//开启事务
    @Override
    public Result update(CategoryDTO categoryDTO) {
        if (categoryDTO == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
        return Result.success();
    }

    /**
     * 启用禁用
     *
     * @param status
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result updateStatus(Integer status, Long id) {
        if (status == null || id == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        Category category = new Category().builder()
                .status(status)
                .id(id)
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.update(category);
        return Result.success();
    }

    /**
     * 新增
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public Result addCteg(CategoryDTO categoryDTO) {
        if (categoryDTO.getName() == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        Category ctegoryMapperByName = categoryMapper.getByName(categoryDTO.getName());
        if (ctegoryMapperByName != null) {

            return Result.error(MessageConstant.DISHNAME_EXIT_FOUND);

        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setCreateTime(LocalDateTime.now());
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.add(category);
        return Result.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result delete(Long id) {
        //非空判断
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //根据id判断菜品分类和套餐是否包含菜品和套餐

        List<Dish> dishList = dishMapper.getByCategoryId(id);
        if (dishList.size() > 0) {
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        List<Setmeal> setmealList = setmealMapper.getById(id);
        if (setmealList.size() > 0) {
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
        return Result.success();
    }

    /**
     * 分类列表
     *
     * @param type
     * @return
     */
    @Override
    public Result list(Integer type) {

        List<Category> list = categoryMapper.list(type);

        return Result.success(list);
    }
}